<?php
class Rss_IndexController extends Zendvn_Controller_Action{
	
	public function init(){
		parent::init();
		$this->view->main = FALSE;
		
	}
	public function indexAction(){
		
		
		if($this->getRequest()->isPost()){
			
			
		$post = new Post_Model_Index();				
		$category = new Category_Model_Index();
		$auth = new Registration_Model_Auth();
		
		
		$name = $_POST["category"];
		$email = $_POST["email"];
		
		$data_category = $category->getItemByName(strtolower($name));
		
		if (strtolower($name) == "car"){
			
			
				
			if ($post->checkRss($data_category["id"])){
				echo '{"category":"no"}';	
				return ;
			}

			$arrFeed = array('{"email":"rssvnexpress@vnexpress.com","url":"http://vnexpress.net/rss/oto-xe-may.rss"}','{"email":"rssdantri@dantri.com","url":"http://dantri.com.vn/otoxemay.rss"}');
			
		}
		elseif (strtolower($name) == "fashion"){

			$arrFeed = array('{"email":"rssdantri@dantri.com","url":"http://dantri.com.vn/thoi-trang.rss"}','{"email":"rssoh@oh.com","url":"http://www.ohthoitrang.com/rss/tuoi_teen"}','{"email":"rssoh@oh.com","url":"http://www.ohthoitrang.com/rss/cong_so"}');
			
		}
		elseif (strtolower($name) == "travel"){

			$arrFeed = array('{"email":"rssdantri@dantri.com","url":"http://dantri.com.vn/du-lich-kham-pha.rss"}','{"email":"rssoh@oh.com","url":"http://www.ohthoitrang.com/rss/tuoi_teen"}','{"email":"rssoh@oh.com","url":"http://www.ohthoitrang.com/rss/cong_so"}');
			
		}
		else {

			$arrFeed = array('{"email":"rssdantri@dantri.com","url":"http://dantri.com.vn/cong-nghe.rss"}');
			
		}
		
		
		
		foreach($arrFeed as $key => $item){
			$data = json_decode($item);
			
			
			$feed = $data->url;
			$email = $data->email;
			// import a feed from a uri
			
			
		
		
		$zendFeed = Zend_Feed_Reader::import($feed);
		
		// Store data in array
		$data = array(
		'title'        => $zendFeed->getTitle(),
		'link'         => $zendFeed->getLink(),
		'dateModified' => $zendFeed->getDateModified(),
		'description'  => $zendFeed->getDescription(),
		'language'     => $zendFeed->getLanguage(),
		'entries'      => array(),
		);
		
			
		
		
		foreach ($zendFeed as $entry) {
			
			
			
			
			$type = "rss";
			$status = "publish";
			
			
			$title = $entry->getTitle();
			$content = $entry->getContent();
			
			$user = $auth->getItemByUsername($email);
			$link = $entry->getLink();
			
			$idcategory = $data_category["id"];
			
			
			$arr = array(
					"userid"=>$user["id"],
					"title"=>trim(strip_tags($title)),
					"categoryid"=>$idcategory,
					"content"=>trim(strip_tags($content)),
					"status"=>$status,
					"type"=>$type,
					"link"=>$link,
					"user_wall_id"=>"",
					"chanelid"=>"",
					"createdate"=>date('Y-m-d h:i:s',time()),										
			);
			
			
			
			
			$idpost = $post->insertPost($arr);
			
			// Media
			
			$media = new Post_Model_Media();
			
			$xpath = new DOMXPath(@DOMDocument::loadHTML($content));
			$src = $xpath->evaluate("string(//img/@src)");
			
			

			if ($email == "rssoh@oh.com"){
				$src = str_replace("http://www.ohthoitrang.com/","",$src);
				$src = "http://www.ohthoitrang.com/".$src;
			}
       		
			
			
       		$arr = array(
				"userid"=>$user["id"],
				"link"=>$src,
				"description"=>"",
		        "postid"=>$idpost,										
			);
				
			$media->insertMedia($arr);
			
				
			
		}
		
		
			
		}
		
		echo '{"category":"'.strtolower($name).'"}';
			
		// Output feed data to screen.
		//var_dump($data);
			
		
		}
		
	}
	
	public function viewAction(){
		
	}
	
}